package org.csu.jpetstore.controller;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("account/")
@SessionAttributes({"account", "authenticated"})
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping("help")
    public String help(){
        return "/help";
    }

    @GetMapping("main")
    public String viewMain(){
        return "catalog/Main";
    }

    @GetMapping("searchusername")
    public boolean searchusername(String uname, Model model){
        Account account = accountService.getAccount(uname);
        if(account == null){
            String tip = null;
            model.addAttribute(tip);
            return false;
        }else{
            String tip = "该账号已存在";
            model.addAttribute(tip);
            return true;
        }
    }


    @GetMapping("accountDate")
    public String accountData(){
        return "account/AccountField";
    }

    @PostMapping("accountDate")
    public String accountData(@SessionAttribute("account") Account account, Model model){
        String tip = accountService.verifyEligibility(account);
        if(tip != null) {
            model.addAttribute("account", account);
            accountService.reviseAccount(account);
            tip = "保存成功";
            model.addAttribute("tip", tip);
        }
        return "account/AccountField";
    }

    @GetMapping("register")
    public String register(Model model){
        model.addAttribute("newAccount", new Account());
        return "account/Register";
    }

    @PostMapping("register")
    public String register(@SessionAttribute("account") Account account, Model model){
        String tip = account.someEmpty();
        if(tip == null){
            accountService.loginNewAccount(account);
            model.addAttribute("account", account);
        }else{
            return "account/Register";
        }
        return "catalog/Main";
    }

    @GetMapping("signin")
    public String signin(){
        return "account/Signinform";
    }

    @PostMapping("signin")
    public String signin(String username, String password, Model model){
        Account account = accountService.getAccount(username, password);
        if(account != null){
            boolean authenticated = true;
            model.addAttribute("authenticated", authenticated);
            model.addAttribute("account", account);
            return "/catalog/Main";
        }else{
            String tip = "账号或密码错误";
            model.addAttribute("tip", tip);
            return "account/SigninForm";
        }
    }

    @GetMapping("signout")
    public String signout(Account account, Model model){
        if(account != null){
            account.setStatus("0");
        }
        boolean authenticated = false;
        model.addAttribute("authenticated", authenticated);
        return "account/Signout";
    }

    @GetMapping("validation")
    public void validation(HttpServletRequest request, HttpServletResponse response){
        accountService.setAuthCode(request, response);
    }
}
